package com.cm.log_process.log_query.handler;


import com.cm.log_process.log_common.entity.Log;
import com.cm.log_process.log_common.entity.PageResult;
import com.cm.log_process.log_common.entity.Result;
import com.cm.log_process.log_common.entity.StatusCode;
import com.cm.log_process.log_common.exp.IllegalPhoneException;
import com.cm.log_process.log_common.exp.NoIndexException;
import com.cm.log_process.log_common.exp.ParamNotMatchException;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.search.query.QueryPhaseExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * Creator:郭兆鹏,沈万龙
 * Date:2019/12/11
 * 思路：全局异常处理
 */
@Order(-1)
@ControllerAdvice
public class GlobalExceptionHandler{

    private static final Logger logger =
            LoggerFactory.getLogger(GlobalExceptionHandler.class);

    //未输入索引名异常
    @ExceptionHandler(value = NoIndexException.class)
    @ResponseBody
    public Result<Log> noIndexExp(NoIndexException ex){
        return new Result<>(StatusCode.PARAM_NEED.getCode(),new PageResult<>(),ex.getMessage());
    }

    @ExceptionHandler(value = ParamNotMatchException.class)
    @ResponseBody
    public Result<Log> paramNotMatch(ParamNotMatchException ex){
        return new Result<>(StatusCode.PARAM_NEED.getCode(),new PageResult<>(),ex.getMessage());
    }


    @ExceptionHandler(value = NumberFormatException.class)
    @ResponseBody
    public Result<Log> numberFormatExcp(NumberFormatException ex){
        return new Result<>(StatusCode.ERROR.getCode(),ex.getMessage());
    }

    @ExceptionHandler(value = IllegalPhoneException.class)
    @ResponseBody
    public Result<Log> illegalPhoneExp(IllegalPhoneException ex){
        return new Result<>(StatusCode.ERROR.getCode(),"请输入正确的手机号");
    }



    @ExceptionHandler
    @ResponseBody
    public Map<String, Object> exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
        Map<String, Object> map = new HashMap<>();
        if (e instanceof MissingServletRequestParameterException) {
            map.put("code",StatusCode.PARAM_NEED.getCode());
            map.put("message", "缺少请求参数");
        }
        if (e instanceof IllegalArgumentException) {
            map.put("code",StatusCode.PARAM_NEED.getCode());
            map.put("message", "参数异常");
        }
        if (e instanceof IndexNotFoundException){
            map.put("code",StatusCode.PARAM_NEED.getCode());
            map.put("message","找不到输入的项目名");
        }
        if(e instanceof HttpMessageNotReadableException){
            map.put("code",StatusCode.ORTHER.getCode());
            map.put("message","请重新确认你请求参数的格式是否输入正确");
        }

        if((e instanceof QueryPhaseExecutionException) || (e instanceof SearchPhaseExecutionException)){
            map.put("code",StatusCode.ERROR.getCode());
            map.put("message","请重试更精确的查询条件，无法进行深度分页查询");
        }
        e.printStackTrace();
        return map;
    }
}
